package com.lhd.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.util.RandomUtil;
import com.lhd.component.RedisComponent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import com.lhd.entity.enums.PageSize;
import com.lhd.entity.query.UserQuery;
import com.lhd.entity.po.User;
import com.lhd.entity.vo.PaginationResultVO;
import com.lhd.entity.query.SimplePage;
import com.lhd.mappers.UserMapper;
import com.lhd.service.UserService;

/**
 * @Author: liuhd
 * @Date: 2025/03/16
 * @Description: User业务接口实现类
 */
@Service
public class UserServiceImpl implements UserService { 
	@Resource
	private UserMapper<User, UserQuery> userMapper;
	@Resource
	private JavaMailSender javaMailSender;
	@Value("${spring.mail.username}")
	private String from;
	@Resource
	private RedisComponent redisComponent;
	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<User> findListByQuery(UserQuery query){
		return userMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	@Override
	public Long findCountByQuery(UserQuery query){
		return userMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	@Override
	public PaginationResultVO<User> findListByPage(UserQuery query){
		// 查出查询的结果数量
		long count = findCountByQuery(query);
		// 如果没给pageSize 默认是15
		long pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();
		// 构建SimplePage 可以确定limit的index与offset以及总页数 以及真正的pageNo
		SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
		// 设置到query中 xml中会使用query.simplePage.start与query.simplePage.end
		query.setSimplePage(page);
		// 用query查询 此时是分页条件查询
		List<User> list = this.findListByQuery(query);
		// 将结果塞到PaginationResultVO中
		PaginationResultVO<User> result = new PaginationResultVO<>(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Long add(User bean){
		return userMapper.insert(bean);
	}

	/**
	 * 新增或更新
	 */
	@Override
	public Long addOrUpdate(User bean){
		return userMapper.insertOrUpdate(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Long addBatch(List<User> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0L;
		}
		return userMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或更新
	 */
	@Override
	public Long addOrUpdateBatch(List<User> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0L;
		}
		return userMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Long updateByQuery(User bean, UserQuery query){
		return userMapper.updateByQuery(bean, query);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Long deleteByQuery(UserQuery query){
		return userMapper.deleteByQuery(query);
	}

	/**
	 * 根据PRIMARY查询
	 */
	@Override
	public User getUserById(Integer id){
		return userMapper.selectById(id);
	}

	/**
	 * 根据PRIMARY删除
	 */
	@Override
	public Long deleteUserById(Integer id){
		return userMapper.deleteById(id);
	}

	/**
	 * 根据PRIMARY更新
	 */
	@Override
	public Long updateUserById(User bean, Integer id){
		return userMapper.updateById(bean, id);
	}

	@Override
	public User findUserByPhone(String phone) {
		return userMapper.selectByPhone(phone);
	}

	@Override
	public void sendCode(String email) {
		// 生成验证码
		String code = RandomUtil.randomNumbers(6);
		// 将验证码发往邮箱
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(from);
		simpleMailMessage.setTo(email);
		simpleMailMessage.setSubject("Q-SIGN密码找回");
		simpleMailMessage.setText("您的验证码是：" + code + "，请妥善保管。");
		javaMailSender.send(simpleMailMessage);
		// 验证码保存到redis
		redisComponent.saveMailCode(email,code);
	}
}